package com.mall.biz;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.mall.common.util.bit.PropertiesConfigure;
import com.mall.constant.PropertiesKey;
import com.mall.web.util.StringUtil;


@Service
public class ErWeiMaBizImpl {

	@Autowired
	private PropertiesConfigure propertiesConfigure;

	public void createErWeiMa(Long userId) throws Exception {
		String filePath = propertiesConfigure.getProperties(PropertiesKey.userfilepath);
		filePath = filePath + "/" + StringUtil.getPicPath(userId);
		String fileName = "2wm_" + userId + ".png";
		File dir = new File(filePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		String content = "http://www.caidijia.com/user/register.html?parentId=" + userId;
		int width = 200; // 图像宽度
		int height = 200; // 图像高度
		String format = "png";// 图像类型
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix;

		bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		Path path = FileSystems.getDefault().getPath(filePath, fileName);
		MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
	}

	/**
	 * 生成桌号的二维码
	 * 
	 * @param mallId
	 * @param num
	 * @throws Exception
	 */
	public void createErWeiMaForTableQrcode(Long mallId, Long tableNumId, Long dateNow, String num) throws Exception {
		String filePath = propertiesConfigure.getProperties(PropertiesKey.userfilepath);
		filePath = filePath + "/qrcode";
		String fileName = "t" + mallId + tableNumId + dateNow + ".png";
		File dir = new File(filePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		String content = propertiesConfigure.getProperties(PropertiesKey.httpbeginurl) + "/zhihui/zhihuiMallDetail.htm?mallId=" + mallId + "&tableNum=" + num;
		int width = 300; // 图像宽度
		int height = 300; // 图像高度
		String format = "png";// 图像类型
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix;
		bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		Path path = FileSystems.getDefault().getPath(filePath, fileName);
		MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
	}

	/**
	 * 生成店铺的二维码
	 * 
	 * @param mallId
	 * @param dateNow
	 * @throws Exception
	 */
	public Boolean createErWeiMaForMallQrcode(Integer onlyKey, Long dateNow) throws Exception {
		String filePath = propertiesConfigure.getProperties(PropertiesKey.userfilepath);
		filePath = filePath + "/qrcode";
		String fileName = "weishang" + onlyKey + dateNow + ".png";
		File dir = new File(filePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		String content =  null;
		if (onlyKey ==6) {
			content = "http://www.maigedian.net/weishang/user/login.htm?onlyKey=" + onlyKey;
		}else{
			 content = propertiesConfigure.getProperties(PropertiesKey.httpbeginurl)+"/weishang/user/login.htm?onlyKey=" + onlyKey;
		}
		
		int width = 300; // 图像宽度
		int height = 300; // 图像高度
		String format = "png";// 图像类型
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix;

		bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		Path path = FileSystems.getDefault().getPath(filePath, fileName);
		MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
		return true;
	}

}
